package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerTraceHistoryMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTraceHistoryService implements ICustomerTraceHistoryService {
    @Autowired
    private CustomerTraceHistoryMapper customerTraceHistoryMapper;
    @Override
    public void saveOrUpdate(CustomerTraceHistory customerTraceHistory) {
        if(customerTraceHistory.getId() != null){

            customerTraceHistoryMapper.updateByPrimaryKey(customerTraceHistory);
        } else {
            //新建的对象  没有录入人,以及录入日期
            customerTraceHistory.setInputTime(new Date());
            customerTraceHistory.setInputUser((Employee) SecurityUtils.getSubject().getPrincipal());
            customerTraceHistoryMapper.insert(customerTraceHistory);
        }
    }


    @Override
    public CustomerTraceHistory getCustomerTraceHistoryById(Long id) {
        return customerTraceHistoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTraceHistory> getAll() {
        return customerTraceHistoryMapper.selectAll();
    }

    @Override
    public PageInfo<CustomerTraceHistory> selectByQuery(QueryObject queryObject) {
//        int totalCount = customerTraceHistoryMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        //用pageInfo   调用方法
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<CustomerTraceHistory> customerTraceHistorys = customerTraceHistoryMapper.selectByQuery(queryObject);
        return new PageInfo<>(customerTraceHistorys);
    }
}
